//
//  DzAuthorizationHelper.m
//  dzyc
//
//  Created by 吴斌 on 2024/6/18.
//

#import "DzAuthorizationHelper.h"
#import <CoreLocation/CoreLocation.h>
@interface DzAuthorizationHelper()<CLLocationManagerDelegate>
@property (nonatomic, strong)CLLocationManager * dzLocationManager;
@end
@implementation DzAuthorizationHelper
static DzAuthorizationHelper *defaultManager = nil;
+(DzAuthorizationHelper*)defaultManager{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        if(defaultManager == nil){
            defaultManager = [[self alloc] init];
        }
    });
    return defaultManager;
}
- (CLLocationManager *)dzLocationManager{
    if(!_dzLocationManager){
        _dzLocationManager = [[CLLocationManager alloc] init];
        [_dzLocationManager requestWhenInUseAuthorization];
        [_dzLocationManager requestAlwaysAuthorization];
        [_dzLocationManager startUpdatingLocation];
        _dzLocationManager.delegate = self;
    }
    return _dzLocationManager;
}
//定位权限
+ (void)requestLocationAuthorization:(void(^)(DzAuthorizationStatus status))callback{
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    if (authStatus == kCLAuthorizationStatusNotDetermined) {
        [[DzAuthorizationHelper defaultManager].dzLocationManager startUpdatingLocation];
    } else if (authStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self executeCallback:callback status:DzAuthorizationAuthorized];
        [[DzAuthorizationHelper defaultManager].dzLocationManager startUpdatingLocation];
    } else if (authStatus == kCLAuthorizationStatusDenied) {
        [self executeCallback:callback status:DzAuthorizationDenied];
    } else if (authStatus == kCLAuthorizationStatusRestricted) {
        [self executeCallback:callback status:DzAuthorizationRestricted];
    }
}
+ (void)executeCallback:(void (^)(DzAuthorizationStatus))callback status:(DzAuthorizationStatus)status {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (callback) {
            callback(status);
        }
    });
}
#pragma mark CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    CLLocation *newLocation = locations.firstObject;
    NSLog(@"经度%f，纬度%f",newLocation.coordinate.longitude,newLocation.coordinate.latitude);
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0){
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
             //获取城市
             NSString *city = placemark.locality;
             if (!city) {
                 //四大直辖市的城市信息无法通过locality获得，只能通过获取省份的方法来获得（如果city为空，则可知为直辖市）
                 city = placemark.administrativeArea;
             }
             NSLog(@"location -- 省:%@, 市：%@， 区：%@ 街道：%@", placemark.administrativeArea, city, placemark.subLocality, placemark.name);
        }
    }];
}
@end
